package org.edu.controller.manage;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.edu.bean.TmPersoninfo;
import org.edu.common.PageView;
import org.edu.common.QueryResult;
import org.edu.controller.BaseAction;
import org.edu.service.manage.PersonManageService;

/**
 * 
 * @author 杨霞
 * 
 *         纳税人基本信息数据维护控制类
 * 
 *         2011-4-25
 */
@SuppressWarnings("serial")
public class PersonManageController extends BaseAction {
	@Resource
	private PersonManageService service;
	private TmPersoninfo entity;

	public PersonManageService getService() {
		return service;
	}

	public void setService(PersonManageService service) {
		this.service = service;
	}

	public TmPersoninfo getEntity() {
		return entity;
	}

	public void setEntity(TmPersoninfo entity) {
		this.entity = entity;
	}

	/**
	 * 分页显示所有的纳税人基本信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		PageView<TmPersoninfo> pageView = new PageView<TmPersoninfo>(20, page);
		QueryResult<TmPersoninfo> queryResult = service.getQueryResult(pageView
				.getFirstResult(), pageView.getMaxresult());
		pageView.setQueryResult(queryResult);
		ServletActionContext.getRequest().setAttribute("pageView", pageView);
		return SUCCESS;
	}

	/**
	 * 按照条件查询纳税人基本信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String findPersonInfo() throws Exception {
		PageView<TmPersoninfo> pageView = new PageView<TmPersoninfo>(20, page);
		QueryResult<TmPersoninfo> queryResult = new QueryResult<TmPersoninfo>();
		Object[] prams = new Object[1];
		if (conditionValue != null && !"".equals(conditionValue.trim())) {
			prams[0] = "%" + conditionValue.trim() + "%";
			String hqlCondition = "";
			if (condition.equals("按纳税人名称查询")) {
				hqlCondition = "o.taxplayer like ?";
			} else if (condition.equals("按纳税人识别码查询")) {
				hqlCondition = "o.pcode like ?";
			}
			queryResult = service.getQueryResult(pageView.getFirstResult(),
					pageView.getMaxresult(), hqlCondition, prams);
			if (queryResult.getTotalrecord() == 0) {
				addActionMessage("系统不存在此类信息，请检查查询条件！");
				return "message";
			} else {

				pageView.setQueryResult(queryResult);
				ServletActionContext.getRequest().setAttribute("pageView",
						pageView);
				return SUCCESS;
			}
		} else {
			addActionMessage("查询条件不能为空！");
			return "message";
		}
	}

	/**
	 * 跳转到纳税人基本信息修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	public String forwardToUpdate() throws Exception {
		entity = service.find(entity.getId());
		return "forward";
	}

	/**
	 * 修改纳税人基本信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws Exception {

		try {
			System.out.println(entity.getId());
			service.updatePerson(entity);
			addActionMessage("修改纳税人【" + entity.getTaxplayer() + "】的信息成功！");

		} catch (Exception e) {
			addActionMessage("修改纳税人【" + entity.getTaxplayer() + "】的信息失败！");
		}

		return "message";
	}

	/**
	 * 删除纳税人基本信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws Exception {
		try {
			entity = service.find(entity.getId());
			service.deletePerson(entity);
			addActionMessage("删除纳税人【" + entity.getTaxplayer() + "】的信息成功！");
		} catch (Exception e) {
			addActionMessage("删除纳税人【" + entity.getTaxplayer() + "】的信息失败！");
		}
		return "message";
	}
	/**
	 * 查看纳税人详细信息
	 * @return
	 * @throws Exception
	 */
	public String show() throws Exception {
		entity = service.find(entity.getId());
		return "show";
	}
}
